package com.projectmanagement.server.service.serviceImpl;


import com.alibaba.fastjson.JSON;
import com.projectmanagement.server.dao.entity.bo.PageObject;
import com.projectmanagement.server.dao.entity.po.CaseDetail;
import com.projectmanagement.server.dao.entity.po.TaskPlan;
import com.projectmanagement.server.dao.mapper.CaseDetailMapper;
import com.projectmanagement.server.service.enums.UserRoleEnum;
import com.projectmanagement.server.service.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description
 * @author 星空梦语
 * @date 2021/3/9 下午4:08
 */
@Service
public class CaseService {

    @Resource
    private CaseDetailMapper caseDetailMapper;


    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public String queryAll(HttpServletRequest request,PageObject pageObject) {
        int offset = (pageObject.getOffset() - 1) * pageObject.getLimit();
        HttpSession session=request.getSession();
        String name=session.getAttribute("name").toString();
        int role= Integer.parseInt(session.getAttribute("role").toString());
        int count=0;
        List<CaseDetail> caseDetailList=new ArrayList<>();
        if(role==UserRoleEnum.RD.getFlag()){
            count= caseDetailMapper.queryPageCount(pageObject.getData().toString(),name);
            caseDetailList = caseDetailMapper.queryPageData(offset, pageObject.getLimit(), pageObject.getData().toString(),name);
        }else{
            count= caseDetailMapper.queryPageCount(pageObject.getData().toString(),null);
            caseDetailList = caseDetailMapper.queryPageData(offset, pageObject.getLimit(), pageObject.getData().toString(),null);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", caseDetailList);
        return JSON.toJSONString(map);
    }

    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public List<CaseDetail> queryChild(HttpServletRequest request,PageObject pageObject) {
        HttpSession session=request.getSession();
        String case_id=pageObject.getData().toString();
        Example example=new Example(CaseDetail.class);
        example.createCriteria().andEqualTo("parentId",case_id);
        return caseDetailMapper.selectByExample(example);
    }




    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public void removeCase(PageObject pageObject) {
        CaseDetail caseDetail = JSON.parseObject(JSON.toJSONString(pageObject.getData()), CaseDetail.class);
        caseDetailMapper.deleteByPrimaryKey(caseDetail.getId());
        removeChildren(caseDetail.getCaseId());
    }

    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public void removeChildren(String case_id){
        Example example=new Example(CaseDetail.class);
        example.createCriteria().andEqualTo("parentId",case_id);
        List<CaseDetail> caseDetailList=caseDetailMapper.selectByExample(example);
        System.out.println(JSON.toJSONString(caseDetailList));
        for (CaseDetail caseDetail : caseDetailList) {
            caseDetailMapper.deleteByPrimaryKey(caseDetail.getId());
            removeChildren(caseDetail.getCaseId());
        }

    }




    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public void saveCase(PageObject pageObject) {
        CaseDetail caseDetail = JSON.parseObject(JSON.toJSONString(pageObject.getData()), CaseDetail.class);
        int count = caseDetailMapper.queryCountByExample(caseDetail.getCaseId());
        if (count <= 0) {
            caseDetailMapper.insertSelective(caseDetail);
            //插入子级的同时更新父级has_children字段值为1
            caseDetailMapper.updateParent(caseDetail.getParentId());
        } else {
            caseDetailMapper.updateByPrimaryKey(caseDetail);
        }
    }


    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public void runCase(HttpServletRequest request,PageObject pageObject) throws Exception {
        HttpSession session=request.getSession();
        String name=session.getAttribute("name").toString();
        int role= Integer.parseInt(session.getAttribute("role").toString());
        Map map = (Map) pageObject.getData();
        String result = map.get("result").toString();
        int id = (int) map.get("id");
        CaseDetail caseDetail=new CaseDetail();
        caseDetail.setId(id);
        caseDetail.setCaseProgress("9");
        if(role==UserRoleEnum.TESTER.getFlag()){
            caseDetail.setTesterRun(name);
            caseDetail.setTesterRunResult(result);
            caseDetail.setTesterRunTime(DateUtil.formatToDayByDate3(new Date()));
        }else{
            caseDetail.setRdRun(name);
            caseDetail.setRdRunResult(result);
            caseDetail.setRdRunTime(DateUtil.formatToDayByDate3(new Date()));
        }
        caseDetailMapper.updateByPrimaryKeySelective(caseDetail);
    }
}
